package com.cn.yunhe.rentcars.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BaclistMapper;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.model.Baclist;
import com.cn.yunhe.rentcars.model.ClientUser;
import com.cn.yunhe.rentcars.model.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * 黑名单管理
 * @author 刘建国
 *
 */
@RestController
public class BaclistController {
	
	@Autowired
	private BaclistMapper baclistMapper;
	@Autowired
    private BusiLogMapper busiLogMapper;
	/**
	 * 跳转到客户黑名单管理页面
	 * @return 页面
	 *
	 */
	@RequestMapping(value="/baclists", method=RequestMethod.GET)
	public ModelAndView showPageBaclist(){
		
		return new ModelAndView("baclist-list") ;
	}
	
	@RequestMapping(value="/baclist-add", method=RequestMethod.GET)
	public ModelAndView showAddClient(){
		System.out.println("aa");
		return new ModelAndView("baclist-add");
	}
	
	@RequestMapping(value="/baclist-edit",method=RequestMethod.GET)
	public ModelAndView showBaclist(@RequestParam String bid){
		Baclist baclist = baclistMapper.getBaclistById(bid);
		Map<String,Baclist> modelMap = new HashMap<String,Baclist>();
		modelMap.put("baclist",baclist );
		return new ModelAndView("baclist-edit",modelMap);
	}
		
		/*
		 * 查询用户
		 * 
		 * 
		 */
		
		@RequestMapping(value="/userBaclist", method=RequestMethod.GET)
		public Map<String,Object> getBaclistCond(@RequestParam String name,@RequestParam String mobile,@RequestParam String idcard,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
			
			//TODO:把输入参数封装费Map
			Map<String,String> paramsMap = new HashMap<String,String>();
			paramsMap.put("name", name);
			paramsMap.put("mobile", mobile);
			paramsMap.put("idcard", idcard);
			
			int pageNum = (start/length)+1;	//获取当前查询第几页
			int pageSize = length;	//每页多少条记录
			
			//TODO:调用DAO MAPPER数据层传入MAP参数去模糊查询，返回List结构
			PageHelper.startPage(pageNum,pageSize);
			List<Baclist> baclist = baclistMapper.searchBaclists(paramsMap);
			PageInfo<Baclist> page = new PageInfo<Baclist>(baclist);
			
			Map<String,Object> rsMap = new HashMap<String,Object>();
			rsMap.put("draw",draw);
		    rsMap.put("data", page.getList());	      
		    rsMap.put("recordsTotal", page.getTotal());
		    rsMap.put("recordsFiltered", page.getTotal());
			return rsMap;
			
		}
			
			/**
			 * 添加用户
			 * @param clientUser
			 */
			@RequestMapping(value="/addBaclist",method=RequestMethod.POST)
			public void addBaclist(@RequestBody Baclist baclist,HttpServletRequest request){
				baclist.setBid(UUID.randomUUID().toString());
				System.out.println("uyhgyj");
				baclistMapper.addBaclist(baclist);
				//添加日志信息
		        Map<String,Object> busMap = new HashMap<String,Object>();
		        busMap.put("logid", UUID.randomUUID().toString());
		        busMap.put("businame", "添加客户黑名单信息");
		        busMap.put("busitype", "添加");
		        busMap.put("busiconent","添加客户黑名单信息");
		        BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
		
			}
		
			/**
			 * 删除用户
			 * @param bid
			 */
			@RequestMapping(value="/delBaclist",method=RequestMethod.POST)		
			public void delBaclist(@RequestParam String bid,HttpServletRequest request){
				baclistMapper.delBaclist(bid);
				//System.out.println("删除成功");
				//添加日志信息
                Map<String,Object> busMap = new HashMap<String,Object>();
                busMap.put("logid", UUID.randomUUID().toString());
                busMap.put("businame", "删除客户黑名单");
                busMap.put("busitype", "删除");
                busMap.put("busiconent","删除客户黑名单");
                BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
				
			}
		
		
		
		
			/**
			 * 修改用户
			 * 
			 * @param userid
			 */
			@RequestMapping(value="/editBaclist/{bid}", method=RequestMethod.PUT)
			public void editBaclist(@PathVariable("bid") String bid,@RequestBody Baclist baclist,HttpServletRequest request){
				baclist.setBid(bid);
				baclistMapper.editBaclist(baclist);
				//添加日志信息
                Map<String,Object> busMap = new HashMap<String,Object>();
                busMap.put("logid", UUID.randomUUID().toString());
                busMap.put("businame", "修改客户黑名单信息");
                busMap.put("busitype", "修改");
                busMap.put("busiconent","修改客户黑名单信息");
                BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
			}
		
		
			/**
			 * 批量删除用户信息
			 * 
			 * @param bid
			 * @param 
			 */
			@RequestMapping(value="/delBaclists", method=RequestMethod.POST)
			public void delBaclists(@RequestParam String bid,HttpServletRequest request){ 
				List<String> bidList = Arrays.asList(bid.split(","));  		
				baclistMapper.delBaclists(bidList);
				//添加日志信息
                Map<String,Object> busMap = new HashMap<String,Object>();
                busMap.put("logid", UUID.randomUUID().toString());
                busMap.put("businame", "批量删除客户黑名单信息");
                busMap.put("busitype", "批量删除");
                busMap.put("busiconent","批量删除客户黑名单信息");
                BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
				
				
				
			}
		
		
		
		
		
		
	
}
